//
//	File Name   :	XInputGamePad.cpp
//	Description :	XInputGamePad implementation file.
//	Author		:	William McVicar
//  Mail		:	mcvicar.william@gmail.com
//

//  Precompiled Header
#include "CommonDefines.h"
#if defined( DX10 ) || defined( DX11 )

//  Library Includes

//  Local Includes

//  This includes
#include "XInputGamePad.h"

//	Macros
#define INPUT_DEADZONE ( 0.24f * float( 0x7FFF ) )

//  Static Variables
const int XInputGamePad::sm_kiMaxControllers = 4;

//  Static Function Prototypes

//  Implementation

XInputGamePad::XInputGamePad()
	: m_uiControllerNumber( 0 ) 
{

}

XInputGamePad::~XInputGamePad()
{

}

void XInputGamePad::Poll()
{
	DWORD dwResult = XInputGetState( (DWORD)m_uiControllerNumber, &m_tInputState );
	if( dwResult == ERROR_SUCCESS )
		m_bConnected = true;
	else
		m_bConnected = false;
}

bool XInputGamePad::IsButtonDown( u16 _usButton )
{
	bool bIsDown = ( m_tInputState.Gamepad.wButtons & _usButton ) == _usButton;
	return ( m_bConnected && bIsDown );
}

byte XInputGamePad::GetBatteryLevel()
{
	DWORD dwResult = XInputGetBatteryInformation( (DWORD)m_uiControllerNumber, BATTERY_DEVTYPE_GAMEPAD, &m_tBatteryInformation );
	if( dwResult == ERROR_DEVICE_NOT_CONNECTED )
	{
		m_bConnected = false;
		return BATTERY_LEVEL_EMPTY;
	}

	return m_tBatteryInformation.BatteryLevel;
}

#endif